<?php
/**
 * Created by PhpStorm.
 * User: 87976
 * Date: 2018/1/10
 * Time: 10:47
 */

namespace App\Services;



use App\Repositories\Goods\GoodsRepository;
use App\Repositories\Order\OrderInfoRepository;
use App\Repositories\Users\UsersRepository;

/**
 * Class PromoteService
 * @package App\Services
 */
class PromoteService
{

    /**
     * @var GoodsRepository
     */
    protected $goodsRepository;

    /**
     * @var
     */
    protected $promoteGoods;

    /**
     * @var UserRepository
     */
    protected $userRepository;

    /**
     * @var OrderInfoRepository
     */
    protected $orderInfoRepository;

    /**
     * @var array
     */
    protected $error = [];

    /**
     * PromoteService constructor.
     * @param GoodsRepository $goodsRepository
     * @param UsersRepository $usersRepository
     * @param OrderInfoRepository $orderInfoRepository
     */
    public function __construct(GoodsRepository $goodsRepository, UsersRepository $usersRepository, OrderInfoRepository $orderInfoRepository)
    {
        $this->goodsRepository = $goodsRepository;

        $this->userRepository = $usersRepository;

        $this->orderInfoRepository = $orderInfoRepository;
    }


    /**
     * 使用goods_id获取goods促销信息
     * @author Jerry
     * @param array $goods
     * @return $this|array
     */
    public function getPromoteGoodsByGoodsId(array $goods)
    {
        if (empty($goods)) {
            return $goods;
        }

        $goodsPromote = [];

        foreach ($goods as $key => $value) {
            if (! isset($value['goods_id'])) {
                continue;
            }
            $goods = $this->goodsRepository->findGoodsPromoteInfoByGoodsId($value['goods_id'])->toArray();

            if (empty($goods)) {
                continue;
            }

            $goodsPromote[] = $goods;
        }

        $this->promoteGoods = $goodsPromote;

        return $this;
    }

    /**
     * 检查多个商品促销价格
     * @author Jerry
     * @param array $goods
     * @return $this|bool
     */
    public function checkPromotePriceByGoodsList(array $goods)
    {
        if (empty($goods)) {
            return false;
        }

        $this->promoteGoods = $goods;

        foreach ($this->promoteGoods as $key => $value) {
            $price = $this->getGoodsPromotePrice($value, $value['number']);

            $this->promoteGoods[$key]['promote_price'] = $price;
        }

        return $this;
    }

    /**
     * 检查单个商品促销价格
     * @author Jerry
     * @param array $goods
     * @param int $number
     * @return int
     */
    public function getGoodsPromotePrice(array $goods, $number = 1)
    {
        return $this->checkPromote($goods, $number);
    }


    /**
     * 处理促销商品数据
     * Created by Jerry
     * @param $goodsInfo
     * @return mixed
     */
    public function formatPromoteData($goodsInfo)
    {
        $promote = $this->checkPromote($goodsInfo);

        if ($promote > 0) {
            $goodsInfo['promote_message'] = $this->getPromoteErrorMessage();
            $goodsInfo['promote_price'] = $promote;
            $goodsInfo['promote_start_date'] = date('Y-m-d H:i:s', local_to_time($goodsInfo['promote_start_date']));
            $goodsInfo['promote_end_date'] = date('Y-m-d H:i:s', local_to_time($goodsInfo['promote_end_date']));
        }
        else
        {
            unset($goodsInfo['promote_end_date'], $goodsInfo['promote_start_date'], $goodsInfo['promote_goods_number'], $goodsInfo['promote_today_number'], $goodsInfo['promote_price']);
        }

        return $goodsInfo;
    }

    /**
     * 获取促销商品不生效信息
     * @author Jerry
     * @return string
     */
    public function getPromoteErrorMessage()
    {
        $message = implode(',', $this->error);
        return trim($message, ',');
    }

    /**
     * 检查促销条件是否符合
     * @author Jerry
     * @param array $goodsInfo
     * @param int $number
     * @return int
     */
    protected function checkPromote(array $goodsInfo, $number = 1)
    {
        $price = 0;

        if ($goodsInfo['is_buynow'] == 1) {
            return $price;
        }

        if ($goodsInfo['promote_user_rank'] != 3 && $goodsInfo['promote_user_rank'] != $this->getUserRank()) {
            array_push($this->error, '当前会员等级不可用');
        }



        if (isset($goodsInfo['promote_number']) && $goodsInfo['promote_number'] > 0) {
            $storageNumber = $this->getPromoteGoodsOrderGoodsNumber($goodsInfo);

            if ($goodsInfo['promote_number'] < ($storageNumber + $number)) {
                array_push($this->error, '促销商品已售完');
            }
        }

        return $this->bargainPrice($goodsInfo['promote_price'], $goodsInfo['promote_start_date'], $goodsInfo['promote_end_date']);
    }

    /**
     * 返回已购买指定商品总数
     * @author Jerry
     * @param $goods
     * @return bool|int
     */
    protected function getPromoteGoodsOrderGoodsNumber($goods)
    {
        if (empty($goods)) {
            return false;
        }

        $count = $this->orderInfoRepository->findGoodsPromoteOrderNumber($goods['goods_id'], $goods['promote_start_date'], $goods['promote_end_date']);

        return (int) $count;
    }

    /**
     * 返回生效促销价格
     * @author Jerry
     * @param $price
     * @param $startTime
     * @param $endTime
     * @return int
     */
    protected function bargainPrice($price, $startTime, $endTime)
    {
        if ($price == 0) {
            return 0;
        }

        $time = gm_time();
        if ($time >= $startTime && $time <= $endTime) {
            return $price;
        } else {
            return 0;
        }
    }

    /**
     * @author Jerry
     * @param $name
     * @return mixed
     */
    public function __get($name)
    {
        return $this->$name;
    }

    /**
     * @author Jerry
     * @param $name
     * @param $value
     * @return $this
     */
    public function __set($name, $value)
    {
        $this->$name = $value;
        return $this;
    }

    /**
     * 返回当前登录用户等级
     * @author Jerry
     * @return int
     */
    protected function getUserRank()
    {
//        当前使用测试用户id
        $userRank = $this->userRepository->find(3060);

        if ($userRank->user_rank == 0) {
            $userRank->user_rank = 1;
        }

        return $userRank->user_rank;
    }
}